package com.installImage;

import java.awt.AlphaComposite;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.TexturePaint;
import java.awt.geom.AffineTransform;
import java.awt.geom.GeneralPath;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.imageio.ImageIO;
import javax.imageio.ImageReadParam;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;
import javax.swing.ImageIcon;

public class ImageUtils {
    /**
     * 裁剪(多边形)
     * @param inputFilePath 图片输入路径
     * @param outFilePath 图片输出路径
     * @param x[] x轴坐标点数组
     * @param y[] y轴坐标点数组
     * @param n 坐标点数量
     * @throws IOException
     */
    public static void cutPolygon_Image( String inputFilePath,String outFilePath,int x[],
                                         int y[],int n) throws IOException {
        try {
            BufferedImage image = ImageIO.read(new File(inputFilePath));
            GeneralPath clip = new GeneralPath(GeneralPath.WIND_EVEN_ODD, n);
            //int x[]={860,650,300,1700};
            //int y[]={20,20,1000,1000};
            clip.moveTo(x[0], y[0]);
            for (int i = 1; i < x.length; i++) {
                clip.lineTo(x[i], y[i]);
            }
            clip.closePath();

            Rectangle bounds = clip.getBounds();
            //BufferedImage img = new BufferedImage(bounds.width, bounds.height, BufferedImage.TYPE_INT_BGR);
            BufferedImage img = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_BGR);
            Graphics2D g2d = img.createGraphics();
            //clip.transform(AffineTransform.getTranslateInstance(0, 0));
            g2d.setClip(clip);
            //g2d.translate(0, 0);
            g2d.drawImage(image, 0, 0, null);
            g2d.dispose();

            FileOutputStream out = new FileOutputStream(outFilePath);//输出图片的地址
            ImageIO.write(img, "jpg", out);
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
